from typing import List
from urllib.parse import urljoin
from blog import Post
from feedgen.feed import FeedGenerator


def get_abs_url(url, request):
    """ Returns absolute url by joining post url with base url """
    return urljoin(request.url_root, url)


def make_feed(posts: List[Post]) -> None:
    fg = FeedGenerator()
    fg.id('http://lernfunk.de/media/654321')
    fg.title('Some Testfeed')
    fg.author({'name': 'John Doe', 'email': 'john@example.de'})
    fg.link(href='http://example.com', rel='alternate')
    fg.logo('http://ex.com/logo.jpg')
    fg.subtitle('This is a cool feed!')
    fg.link(href='http://larskiesow.de/test.atom', rel='self')
    fg.language('en')
    fg.contributor(name='John Doe', email='jdoe@example.com')
    # posts.reverse()
    for p in posts[15::-1]:
        fe = fg.add_entry()
        fe.id(str(p.id))
        fe.title(p.title)
        fe.content(p.body)
        fe.link(href=f"http://127.0.0.1:5000/post/{p.id}")

    fg.atom_file('templates/atom.xml')
